package com.ruoyi.project.registration.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.registration.domain.RegRegistration;
import com.ruoyi.project.registration.service.RegRegistrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/registration/regregistration")
public class RegRegistrationController extends BaseController {
    @Autowired
    private RegRegistrationService regRegistrationService;


    /**
     * 新增挂号
     */
    @PreAuthorize("@ss.hasPermi('registration:regregistration:add')")
    @Log(title = "门诊挂号", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult addRegRegistration(@RequestBody RegRegistration regRegistration){
        regRegistration.setCreateBy(SecurityUtils.getUsername());
        regRegistration.setCreateTime(new Date());

        //新增挂号
        int rows = regRegistrationService.insertRegRegistration(regRegistration);

        //成功挂号后，科室挂号数加1


        return toAjax(rows);
    }

    @GetMapping("/all")
    public AjaxResult getAll(){
        return AjaxResult.success(regRegistrationService.getAll());
    }

    /**
     * （收费）更新
     */
    @PutMapping
    @Log(title = "挂号列表", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('registration:registration:edit')")
    public AjaxResult editDept(@RequestBody RegRegistration regRegistration){
        // 设置修改人姓名
        regRegistration.setUpdateBy(SecurityUtils.getUsername());
        int rows=0;
        if("未收费".equals(regRegistration.getRrStatus())){
            regRegistration.setRrStatus("待就诊");
            rows = regRegistrationService.updateRegistration(regRegistration);
            return toAjax(rows);
        }else{
            return AjaxResult.error("不能进行收费");
        }



    }

    /**
     * 主键查询
     */
    @GetMapping("/{rrNo}")
    @PreAuthorize("@ss.hasPermi('registration:registration:query')")
    public AjaxResult getById(@PathVariable Integer rrNo){
        RegRegistration regRegistration = regRegistrationService.getByNo(rrNo);
        return AjaxResult.success(regRegistration);
    }

    /**
     * 多条件查询
     * 分页返回TableDataInfo
     * 不分页返回AjaxResult
     * */
    @PreAuthorize("@ss.hasPermi('registration:registration:query')")
    @GetMapping("/list")
    public TableDataInfo list(RegRegistration regRegistration){
        // 分页相关,处理请求参数pageNum，pageSize等。并使用PageHelper处理分页查询。
        startPage();

        // 调用service查询数据库
        List<RegRegistration> list = regRegistrationService.selectList(regRegistration);

        // 返回
        return getDataTable(list);
    }

}
